//
//  KZBannerView.m
//  KZTools_ObjectC
//
//  Created by 朱凯 on 2018/11/26.
//  Copyright © 2018年 zhukai. All rights reserved.
//

#import "KZBannerView.h"

@implementation KZBannerView 
{
    UIScrollView *_scroll;
    UIPageControl *_pageView;
    
    NSInteger _num;
    CGPoint _sideScale;
    KZBannerViewPageControllerPosition _position;
}

#pragma mark - 初始化
- (instancetype)init {
    
    if (self = [super init]) {
        
        [self setupUI];
    }
    return self;
}

#pragma mark - 创建视图
- (void)setupUI {
    
    _scroll = [UIScrollView new];
    _scroll.layer.masksToBounds = false;
    _scroll.backgroundColor = UIColor.redColor;
    _scroll.delegate = self;
    _scroll.pagingEnabled = true;
    _scroll.showsHorizontalScrollIndicator = false;
    _scroll.showsVerticalScrollIndicator = false;
    [self addSubview:_scroll];
    [_scroll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    _pageView = [UIPageControl new];
    _pageView.userInteractionEnabled = false;
    [self addSubview:_pageView];
}

- (void)layoutSubviews {
    
    [super layoutSubviews];

    if (!_datasource) {
        return;
    }
    
    NSMutableArray *views = [NSMutableArray new];
    for (int i = 0; i < _num * 3; i ++) {
        UIView *view = [_datasource bannerAtIndex:i % 3];
        [views addObject:view];
    }
    
    for (int i = 0; i < views.count; i ++) {
        UIView *view = views[i];
        [_scroll addSubview:view];
        view.frame = CGRectMake(0, 0, _scroll.width, _scroll.height);
        view.left = i * view.width;
    }
    
    _scroll.contentSize = CGSizeMake(_scroll.subviews.lastObject.right, 0);
    _scroll.contentOffset = CGPointMake(_scroll.width * _num, 0);
}

#pragma mark - setter
- (void)setDatasource:(id<KZBannerViewDatasource>)datasource {
    
    _datasource = datasource;
    _num = [datasource numOfBanners];
    _sideScale = [datasource respondsToSelector:@selector(bannerSideScale)] ? [datasource bannerSideScale] : CGPointMake(1, 1);
    _position = [datasource respondsToSelector:@selector(bannerPageControllerPosition)] ? [datasource bannerPageControllerPosition] : KZBannerViewPageControllerPositionNone;
    
    _pageView.numberOfPages = _num;
    if (_position == KZBannerViewPageControllerPositionNone) {
        _pageView.hidden = true;
    } else {
        if (_position == KZBannerViewPageControllerPositionTopLeft) {
            [_pageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self);
                make.left.equalTo(self).offset(10);
            }];
        } else if (_position == KZBannerViewPageControllerPositionTopCenter) {
            [_pageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self);
                make.centerX.equalTo(self);
            }];
        } else if (_position == KZBannerViewPageControllerPositionTopRight) {
            [_pageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self);
                make.right.equalTo(self).offset(-10);
            }];
        } else if (_position == KZBannerViewPageControllerPositionBottomLeft) {
            [_pageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(self);
                make.left.equalTo(self).offset(10);
            }];
        } else if (_position == KZBannerViewPageControllerPositionBottomCenter) {
            [_pageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(self);
                make.centerX.equalTo(self);
            }];
        } else if (_position == KZBannerViewPageControllerPositionBottomRight) {
            [_pageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(self);
                make.right.equalTo(self).offset(-10);
            }];
        }
        
    }
}
- (void)setPageIndicatorTintColor:(UIColor *)pageIndicatorTintColor {
    _pageView.pageIndicatorTintColor = pageIndicatorTintColor;
}
- (void)setCurrentPageIndicatorTintColor:(UIColor *)currentPageIndicatorTintColor {
    _pageView.currentPageIndicatorTintColor = currentPageIndicatorTintColor;
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    [self setScrollBannerSideScale];
}
// 滑动视图已经停止
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    [self setScrollContentOffset];
}

#pragma mark - 辅助方法
// 设置scroll的contentOffset
- (void)setScrollContentOffset {
    
    CGFloat x = _scroll.contentOffset.x;
    // 滑动到最后一个，跳转到第二个
    if (x == _scroll.width * _num * 2) {
        _scroll.contentOffset = CGPointMake(_scroll.width * _num, 0);
    }
    // 滑动到第一个，跳转到倒数第二个
    if (x == _scroll.width * _num - _scroll.width) {
        _scroll.contentOffset = CGPointMake(_scroll.width * _num * 2 - _scroll.width, 0);
    }
    
    // 防止滑的快
    if (x == 0) {
        _scroll.contentOffset = CGPointMake(_scroll.width * _num * 2, 0);
    }
    if (x == _scroll.width * _num * 3 - _scroll.width) {
        _scroll.contentOffset = CGPointMake(_scroll.width * _num * 2 - _scroll.width, 0);
    }
    
    // 设置页数
    _pageView.currentPage = (_scroll.contentOffset.x - _scroll.width * _num) / _scroll.width;
}
// 设置scroll子视图的缩放
- (void)setScrollBannerSideScale {

    CGSize minSize = CGSizeMake(_scroll.bounds.size.width * _sideScale.x, _scroll.bounds.size.height * _sideScale.y);
    CGSize maxSize = _scroll.bounds.size;

    
    // 遍历滑动视图子视图来设置缩放
    for (int i = 0; i < _scroll.subviews.count; i ++) {
        UIView *pageView = _scroll.subviews[i];
        CGPoint center = pageView.center;
        pageView.width = maxSize.width - MIN(fabs(_scroll.contentOffset.x - pageView.left) / _scroll.width, 1) * (maxSize.width - minSize.width);
        pageView.height = maxSize.height - MIN(fabs(_scroll.contentOffset.x - pageView.left) / _scroll.width, 1) * (maxSize.height - minSize.height);
        pageView.center = center;
    }
}


#pragma mark - 销毁
- (void)dealloc {
    NSLog(@"KZBannerView已销毁");
}

@end
